package com.huawei.rbac.web.controller;

import com.huawei.rbac.domain.Employee;
import com.huawei.rbac.query.EmployeeQueryObject;
import com.huawei.rbac.result.PageResult;
import com.huawei.rbac.service.IDepartmentService;
import com.huawei.rbac.service.IEmployeeService;
import com.huawei.rbac.service.IRoleService;
import com.huawei.rbac.util.RequiredRermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiredRermission({"员工列表","employee:list"})
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        PageResult<Employee> result = employeeService.query(qo);
        model.addAttribute("result",result);
        //查询高级查询表单中需要的部门信息
        model.addAttribute("depts", departmentService.listAll());
        return "employee/list";
    }

    @RequestMapping("/delete")
    @RequiredRermission({"员工删除","employee:delete"})
    public String delete(Long id){
        if(id != null){
            employeeService.delete(id);
        }
        return "redirect:/employee/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee d,Long[] roleIds){
        employeeService.saveOrUpdate(d,roleIds);
        return "redirect:/employee/list";
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        if(id != null){
            Employee e = employeeService.get(id);
            model.addAttribute("e",e);
        }
        //查询高级查询表单中需要的部门信息
        model.addAttribute("depts", departmentService.listAll());

        //查询所有的角色信息
        model.addAttribute("roles",roleService.listAll());
        return "employee/input";
    }

}
